//


#import "AM_DeleteHistoryViewController.h"
#import "AM_DeleteHirstoryTableViewCell.h"

@interface AM_DeleteHistoryViewController ()<UITableViewDelegate, UITableViewDataSource>
/** <#Description#>*/
@property (nonatomic, strong) UITableView *tableView;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#Description#>*/
@property (nonatomic, strong) AM_CoreDataView *fmdb;

@end

@implementation AM_DeleteHistoryViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    PostNotifition(@"Hidden");
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    PostNotifition(@"show");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setnavBatTitle:@"History"];
    _fmdb = [[AM_CoreDataView alloc]initWithCoreData:@"DeleteData" modelName:fmdt success:^{} fail:^(NSError * _Nullable error) {}];
    //
    __weak typeof(self) weakSelf = self;
    [self setupLeftAndwqwRightSetting:@"All Delete" block:^{
        [weakSelf allDelete];
    }];
    //
    [self.view addSubview:self.tableView];
    [self getDataSoure];
    
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    AM_DeleteHirstoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[AM_DeleteHirstoryTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight  = kRelativeHeight(50);
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

#pragma mark ----------------- 获取数据 ----------------
- (void)getDataSoure {
    //
    [_fmdb am_readEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results)return ;
        self.dataArray = [results copy];
        
        [self.tableView reloadData];
    } fail:^(NSError * _Nullable error) {
        
    }];
    
}
//MARK:清空记录
- (void)allDelete {
   //
    DeleteData *model = _dataArray.lastObject;
    [_fmdb am_deleteEntity:model key:[NSString stringWithFormat:@"id=1"] success:^{
        //
        [self.navigationController popViewControllerAnimated:YES];
        
    } fail:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
